import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

/**
 * Create by chenhd on 2021/8/28.
 * 方法十七：实际上跟方法14中的阻塞队列类似,理论上,所有阻塞队列都能用来实现,这里单独拿出来实现是为了说明SynchronousQueue的特殊性.
 */
public class ConcurrentTask17 {
    public String str;
    public BlockingQueue<String> synchronousQueue = new SynchronousQueue<>();

    public static void main(String[] args) throws InterruptedException {
        ConcurrentTask17 concurrentTask = new ConcurrentTask17();

        new Thread(concurrentTask::putStr).start();
        concurrentTask.synchronousQueue.take();
        System.out.println(concurrentTask.getStr());

    }

    public void putStr() {
        try {
            TimeUnit.SECONDS.sleep(5);
            this.str = "hello concurrent.";
            synchronousQueue.add("ok");
        } catch (InterruptedException e) {
            //log error
            e.printStackTrace();
        }
    }

    public String getStr() {
        return str;
    }
}
